package com.inossem.allotin.adapter.input;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inossem.allotin.R;
import com.inossem.allotin.activity.TransportInputDetailsActivity;
import com.inossem.allotin.utils.DialogUtils;
import com.inossem.allotin.utils.EditInputFilter;
import com.inossem.allotin.utils.ToastUtils;
import com.inossem.allotin.utils.requestUtils.InossemCallback;
import com.inossem.allotin.utils.requestUtils.RetrofitUtils;
import com.inossem.wms.model.dic.DicWhStorageBin;
import com.inossem.wms.model.dic.DicWhStorageBinKey;
import com.inossem.wms.model.serial.SerialCell;
import com.inossem.wms.vo.BaseBean;
import com.inossem.wms.vo.auth.DicWhStorageTypeTreeVo;
import com.inossem.wms.vo.cus.CusTransportBinVo;
import com.inossem.wms.vo.task.CellDataVo;

import java.math.BigDecimal;
import java.util.List;

import retrofit2.Response;

import static android.view.View.GONE;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_TWO;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_ZERO;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_ZERO_STRING;

/**
 * @author LinH
 */
public class TransportInputBatchAdapter extends RecyclerView.Adapter<TransportInputBatchAdapter.ViewHolder> {
    private boolean isCompile;
    private String outputUnitCode;
    private List<CusTransportBinVo> cusTransportBinVos;
    private Context context;
    private TransportInputDetailsActivity activity;
    private Byte taskOutput;
    private String inputWh;

    public TransportInputBatchAdapter(String inputWh, Byte taskOutput, boolean isCompile, String outputUnitCode, TransportInputDetailsActivity activity) {
        this.inputWh = inputWh;
        this.taskOutput = taskOutput;
        this.isCompile = isCompile;
        this.activity = activity;
        this.outputUnitCode = outputUnitCode;
    }

    public void initDate(List<CusTransportBinVo> cusRFIDMatVoList) {
        this.cusTransportBinVos = cusRFIDMatVoList;
        notifyDataSetChanged();
    }

    public List<CusTransportBinVo> getData() {
        return cusTransportBinVos;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        context = viewGroup.getContext();
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_transport_input_batch, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        CusTransportBinVo cusTransportBinVo = cusTransportBinVos.get(i);
        viewHolder.et.setTag(i);
        if (i % STATUS_TWO == STATUS_ZERO) {
            viewHolder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_white));
        } else {
            viewHolder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_bg));
        }
        viewHolder.tv1.setText(cusTransportBinVo.getBatch());
        viewHolder.tv2.setText(String.valueOf(cusTransportBinVo.getStockQty()));
        InputFilter[] filters = {new EditInputFilter(cusTransportBinVo.getStockQty().doubleValue(), outputUnitCode)};
        viewHolder.et.setFilters(filters);
        viewHolder.et.setText(String.valueOf(cusTransportBinVo.getQty()));
        if (!isCompile) {
            viewHolder.tv2.setVisibility(GONE);
            viewHolder.et.setFocusable(false);
            viewHolder.et.setCursorVisible(false);
            viewHolder.et.setFocusableInTouchMode(false);
            viewHolder.et.setBackground(null);
            viewHolder.et.setTextColor(ContextCompat.getColor(context, R.color.text_gray));
            viewHolder.et.setText(String.valueOf(cusTransportBinVo.getQty()));
        }
        viewHolder.et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                int position = (int) viewHolder.et.getTag();
                try {
                    if (!TextUtils.isEmpty(s)) {
                        cusTransportBinVos.get(position).setQty(new BigDecimal(s.toString()));
                    } else {
                        cusTransportBinVos.get(position).setQty(new BigDecimal(STATUS_ZERO));
                    }
                } catch (Exception e) {
                    viewHolder.et.setText(STATUS_ZERO_STRING);
                }
            }
        });

        //存储类型 目标仓位 存储单元
        TextView[] tvs = new TextView[]{viewHolder.tvTypeCode, viewHolder.tvBinCade, viewHolder.tvCellCode};
        for (TextView tv : tvs) {
            tv.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return cusTransportBinVos.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv1;
        TextView tv2;
        EditText et;
        LinearLayout layout;
        TextView tvTypeCode;
        TextView tvBinCade;
        TextView tvCellCode;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv1 = itemView.findViewById(R.id.tv_1);
            tv2 = itemView.findViewById(R.id.tv_2);
            et = itemView.findViewById(R.id.et);
            layout = itemView.findViewById(R.id.layout);
            tvTypeCode = itemView.findViewById(R.id.tv_type_code);
            tvBinCade = itemView.findViewById(R.id.tv_bin_code);
            tvCellCode = itemView.findViewById(R.id.tv_cell_code);
        }
    }
}